#include "hud_item_triangle_mesh.h"

HUD_ITEM_TRIANGLE_MESH::HUD_ITEM_TRIANGLE_MESH(CASH *cash):QObject(), QGraphicsItemGroup()
{

    this->cash=cash;

    load();
    standart();
}

QPainterPath HUD_ITEM_TRIANGLE_MESH::shape() const
{
    return QPainterPath();
}

QRectF HUD_ITEM_TRIANGLE_MESH::boundingRect() const
{
    return QRectF();
}

void HUD_ITEM_TRIANGLE_MESH::load()
{
    item_blue = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value(DEF_HUD_PATH + "triangle mesh/blue.png"), this);
    item_red = new GRAPHICS_PIXMAP_ITEM(*cash->cash_pixmap->value(DEF_HUD_PATH + "triangle mesh/red.png"), this);

    item_blue->setPos(0, 0);
    item_red->setPos(0, 0);

//blue blinking
    prop_anim_blue = new QPropertyAnimation(this, "transparent", this);

    prop_anim_blue->setDuration(3000);

//red blinking
    prop_anim_red = new QPropertyAnimation(this, "transparent", this);

    prop_anim_red->setDuration(1000);

}

void HUD_ITEM_TRIANGLE_MESH::standart()
{
    item_blue->setVisible(true);
    item_red->setVisible(false);

    prop_anim_blue->stop();
    prop_anim_blue->setLoopCount(-1);

    prop_anim_blue->setKeyValueAt(0, 0.2);
    prop_anim_blue->setKeyValueAt(0.5, 1);
    prop_anim_blue->setKeyValueAt(1, 0.2);

    prop_anim_red->stop();

    prop_anim_blue->setCurrentTime(0);
    prop_anim_blue->start();
}

void HUD_ITEM_TRIANGLE_MESH::damage()
{
    item_blue->setVisible(false);
    item_red->setVisible(true);

    prop_anim_red->stop();
    prop_anim_red->setLoopCount(1);

    prop_anim_red->setKeyValueAt(0, 0);
    prop_anim_red->setKeyValueAt(0.3, 1);;
    prop_anim_red->setKeyValueAt(1, 0.5);

    prop_anim_blue->stop();

    prop_anim_red->setCurrentTime(0);
    prop_anim_red->start();

    connect(prop_anim_red, SIGNAL(finished()), this, SLOT(slot_standart()));
}

void HUD_ITEM_TRIANGLE_MESH::shield_destroyed()
{
    item_blue->setVisible(false);
    item_red->setVisible(true);

    prop_anim_red->stop();
    prop_anim_red->setLoopCount(-1);

    prop_anim_red->setKeyValueAt(0, 0.2);
    prop_anim_red->setKeyValueAt(0.5, 1);
    prop_anim_red->setKeyValueAt(1, 0.2);

    prop_anim_blue->stop();

    prop_anim_red->setCurrentTime(0);
    prop_anim_red->start();
}

void HUD_ITEM_TRIANGLE_MESH::show()
{
    prop_anim_blue->stop();
    prop_anim_blue->setLoopCount(1);

    prop_anim_blue->setStartValue(0);
    prop_anim_blue->setEndValue(1);

    prop_anim_blue->start();

    connect(prop_anim_blue, SIGNAL(finished()), this, SLOT(slot_standart()));
}

void HUD_ITEM_TRIANGLE_MESH::hide()
{
    prop_anim_blue->stop();
    prop_anim_blue->setLoopCount(1);

    prop_anim_blue->setStartValue(prop_anim_blue->currentValue());
    prop_anim_blue->setEndValue(0);

    prop_anim_blue->start();
}

HUD_ITEM_TRIANGLE_MESH::~HUD_ITEM_TRIANGLE_MESH()
{
}

qreal HUD_ITEM_TRIANGLE_MESH::Transparent()
{
    return this->opacity();
}

void HUD_ITEM_TRIANGLE_MESH::setTransparent(qreal value)
{
    this->setOpacity(value);
}

void HUD_ITEM_TRIANGLE_MESH::slot_standart()
{
    standart();
    disconnect(prop_anim_blue, SIGNAL(finished()), this, SLOT(slot_standart()));
    disconnect(prop_anim_red, SIGNAL(finished()), this, SLOT(slot_standart()));
}
